home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Color Picker SDK / Sample Code / ScrapPicker Sample / Sources ƒ / ScrapPicker.c < prev    next >
Encoding:
Text File  |  1997-06-13  |  21.5 KB  |  881 lines  |  [TEXT/CWIE]

  1.  
  2. //===============================================================================
  3. //
  4. //                                 ScrapPicker.c
  5. //
  6. //    Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  7. //  by john calhoun & David Hayward
  8. //
  9. //===============================================================================
  10.  
  11.  
  12. #include <ColorPickerComponents.h>
  13. #include <Gestalt.h>
  14. #include <Resources.h>
  15. #include <Scrap.h>
  16. #include <TextUtils.h>
  17. #include "ScrapPicker.h"        // <-- Project interfaces.
  18. #include "PickerCommon.h"
  19.  
  20.  
  21. #define ASSUME_CURR_ORIG_PROFS_SAME    1
  22.  
  23.  
  24. //=====================================================================  Functions
  25. //---------------------------------------------------------------------  doPickerOpen
  26.  
  27. pascal ComponentResult doPickerOpen (PickerStorageHandle storage, ComponentInstance self)
  28. {
  29.     PickerStoragePtr    pStorage;
  30.     OSErr                theErr;
  31.     
  32.     theErr = noErr;
  33.     
  34.                 // Allocate handle for picker globals.
  35.     storage = (PickerStorageHandle)HappiNewHandleClear(sizeof(PickerStorage), &theErr);
  36.     require(storage, bail);
  37.     
  38.     SetComponentInstanceStorage(self, (Handle)storage);
  39.     
  40.     HLock((Handle)storage);
  41.     pStorage = *storage;
  42.     
  43.                 // Store reference to self (this instance).
  44.     pStorage->myself = self;
  45.     
  46.                 // Initialize a couple fields.
  47.     pStorage->realPicker = false;
  48.     pStorage->baseItem = 1;
  49.     
  50.     HUnlock((Handle)storage);
  51.     
  52. bail:
  53.     
  54.     return theErr;
  55. }
  56.  
  57. //---------------------------------------------------------------------  doPickerClose
  58.  
  59. pascal ComponentResult doPickerClose (PickerStorageHandle storage, ComponentInstance self)
  60. {
  61. #pragma unused (self)
  62.     PickerStoragePtr    pStorage;
  63.     char                wasState;
  64.     
  65.                 // If storage is nil, return.
  66.     if (!storage)
  67.         return noErr;
  68.     
  69.                 // Lock and point to storage.
  70.     wasState = HGetState((Handle)storage);
  71.     HLock((Handle)storage);
  72.     pStorage = *storage;
  73.     
  74.                 // If we were fully set up properly (see PickerOpen()).
  75.     if (pStorage->realPicker)
  76.     {
  77.                 // Handle pattern disposal.
  78.         if (pStorage->useColorPats)
  79.         {
  80.             if (pStorage->newColorPat)
  81.                 DisposePixPat(pStorage->newColorPat);
  82.             if (pStorage->origColorPat)
  83.                 DisposePixPat(pStorage->origColorPat);
  84.         }
  85.         else
  86.         {
  87.             if (pStorage->newColorPat)
  88.                 DisposeHandle((Handle)pStorage->newColorPat);
  89.             if (pStorage->origColorPat)
  90.                 DisposeHandle((Handle)pStorage->origColorPat);
  91.         }
  92.     }
  93.     
  94.                 // Remove globals storage.
  95.     HSetState((Handle)storage, wasState);
  96.     DisposeHandle((Handle)storage);
  97.     
  98.     return noErr;
  99. }
  100.  
  101. //---------------------------------------------------------------------  doPickerCanDo
  102.  
  103. pascal ComponentResult doPickerCanDo (PickerStorageHandle storage, SInt16 selector)
  104. {
  105. #pragma unused (storage)
  106.     
  107.     if ((selector >= kComponentTargetSelect) && (selector <= kComponentOpenSelect))
  108.         return true;
  109.     
  110.     if ((selector >= kInitPicker) && (selector <= kExtractHelpItem))
  111.         return true;
  112.     
  113.     return false;
  114. }
  115.  
  116. //---------------------------------------------------------------------  doPickerVersion
  117.  
  118. pascal ComponentResult doPickerVersion (PickerStorageHandle storage)
  119. {
  120. #pragma unused (storage)
  121.     
  122.     return noErr;
  123. }
  124.  
  125. //---------------------------------------------------------------------  doPickerRegister
  126.  
  127. pascal ComponentResult doPickerRegister (PickerStorageHandle storage)
  128. {
  129. #pragma unused (storage)
  130.     
  131.     return noErr;            // always register
  132. }
  133.  
  134. //---------------------------------------------------------------------  doPickerSetTarget
  135.  
  136. pascal ComponentResult doPickerSetTarget (PickerStorageHandle storage, 
  137.         ComponentInstance topOfCallChain)
  138. {
  139. #pragma unused (storage, topOfCallChain)
  140.     
  141.     return noErr;
  142. }
  143.  
  144. //---------------------------------------------------------------------  doPickerInit
  145. // One thing to note, the baseItem is already set at this time.  So, when
  146. // attempting to access dialog items, the index of these will be offset
  147. // by the amount of baseItem.  The local function GetItemRect() takes that into
  148. // account.
  149.  
  150. pascal ComponentResult doPickerInit (PickerStorageHandle storage, PickerInitData* data)
  151. {
  152.     GDHandle            thisDevice;
  153.     PickerStoragePtr    pStorage;
  154.     char                wasState;
  155.     OSErr                theErr;
  156.     SInt16                resFile;
  157.     
  158.     theErr = noErr;
  159.     resFile = 0;
  160.     
  161.                 // Lock and point to storage.
  162.     wasState = HGetState((Handle)storage);
  163.     HLock((Handle)storage);
  164.     pStorage = *storage;
  165.     
  166.                 // Open our resource fork.
  167.     resFile = OpenComponentResFile((Component)pStorage->myself);
  168.     require_action(resFile > 0, fail, theErr = cantLoadPicker;);
  169.     
  170.                 // Init some fields - port, etc..
  171.     GetPort(&pStorage->port);
  172.     pStorage->flags = data->flags;
  173.     pStorage->visible = false;
  174.     pStorage->active = true;
  175.     pStorage->pictNumber = -1;
  176.     
  177.                 // Set up color depth data.
  178.     thisDevice = GetGDevice();
  179.     pStorage->depth = (*((*thisDevice)->gdPMap))->pixelSize;
  180.     pStorage->useColorPats = (pStorage->depth > 1);
  181.     
  182.                 // Allocate the pixpats.
  183.     if (pStorage->useColorPats)
  184.     {
  185.         pStorage->newColorPat = NewPixPat();
  186.         pStorage->origColorPat = NewPixPat();
  187.     }
  188.     else
  189.     {
  190.         pStorage->newColorPat = (PixPatHandle)NewHandle(sizeof(Pattern));
  191.         pStorage->origColorPat = (PixPatHandle)NewHandle(sizeof(Pattern));
  192.     }
  193.     require_action(pStorage->newColorPat && pStorage->origColorPat, fail, theErr = cantLoadPicker;);
  194.     
  195.                 // We got through this far, so we're a real picker now!
  196.     pStorage->realPicker = true;
  197.     
  198.     theErr = CloseComponentResFile(resFile);
  199.     check(theErr == noErr);
  200.     
  201.     HSetState((Handle)storage, wasState);
  202.     
  203.     return theErr;
  204.     
  205. fail:
  206.     
  207.     if (pStorage->useColorPats)
  208.     {
  209.         if (pStorage->newColorPat)
  210.             DisposePixPat(pStorage->newColorPat);
  211.         if (pStorage->origColorPat)
  212.             DisposePixPat(pStorage->origColorPat);
  213.     }
  214.     else
  215.     {
  216.         if (pStorage->newColorPat)
  217.             DisposeHandle((Handle)pStorage->newColorPat);
  218.         if (pStorage->origColorPat)
  219.             DisposeHandle((Handle)pStorage->origColorPat);
  220.     }
  221.     
  222.                 // Make sure we got a reasonable error code.
  223.     if (theErr == noErr)
  224.         theErr = cantLoadPicker;
  225.     
  226.                 // Don't check the error here - we are already in an error state.
  227.     if (resFile > 0)
  228.         (void) CloseComponentResFile(resFile);
  229.     
  230.     HSetState((Handle)storage, wasState);
  231.     
  232.     return theErr;
  233. }
  234.  
  235. //---------------------------------------------------------------------  doPickerTestGraphicsWorld
  236. // We use this function to see if Color Picker 2.1 is around.  SInce we
  237. // rely on the new Color Picker's eyedropper functionality, we return an
  238. // error if 2.1 is not available.  The effect of this is that the Color
  239. // Picker Manager (CPM) will grey out (disable) our picker's icon.
  240.  
  241. pascal ComponentResult doPickerTestGraphicsWorld (PickerStorageHandle storage, PickerInitData* data)
  242. {
  243. #pragma unused (storage, data)
  244.     SInt32        value;
  245.     OSErr        theErr;
  246.     
  247.     theErr = Gestalt(gestaltColorPickerVersion, &value);
  248.     if (theErr != noErr)
  249.         goto bail;
  250.     
  251.     theErr = (value < 0x0210);
  252.     
  253. bail:
  254.     
  255.     return theErr;
  256. }
  257.  
  258. //---------------------------------------------------------------------  doPickerGetDialog
  259.  
  260. pascal ComponentResult doPickerGetDialog (PickerStorageHandle storage)
  261. {
  262.     PickerStoragePtr    pStorage;
  263.     char                wasState;
  264.     OSErr                theErr;
  265.     DialogPtr            theDialog;
  266.     GrafPtr                wasPort;
  267.     SInt16                resFile;
  268.     
  269.                 // Lock and point to storage.
  270.     wasState = HGetState((Handle)storage);
  271.     HLock((Handle)storage);
  272.     pStorage = *storage;
  273.  
  274.     resFile = OpenComponentResFile((Component)pStorage->myself);
  275.     require(resFile > 0, finish);
  276.  
  277.     HSetState((Handle)storage, wasState);
  278.     
  279.     theDialog = 0L;
  280.     theDialog = GetNewDialog(kPickerDialog, 0L, (WindowRef)-1);
  281.     
  282.     DetachResource((Handle)((WindowRecord *)theDialog)->windowDefProc);
  283.     
  284.     theErr = CloseComponentResFile(resFile);
  285.     check(theErr == noErr);
  286.     
  287.     if (theDialog)
  288.     {
  289.         GetPort(&wasPort);
  290.         SetPort((GrafPtr)theDialog);
  291.         
  292.                 // Set up the origin of the dialog.
  293.         SetOrigin(-70, -40);
  294.         SetPort(wasPort);
  295.     }
  296.     
  297. finish:
  298.     
  299.     return (SInt32)theDialog;
  300. }
  301.  
  302. //---------------------------------------------------------------------  doPickerGetItemList
  303.  
  304. pascal ComponentResult doPickerGetItemList (PickerStorageHandle storage)
  305. {
  306.     OSErr        theErr;
  307.     Handle        theItems;
  308.     SInt16        resFile;
  309.     
  310.     theItems = 0L;
  311.     
  312.     resFile = OpenComponentResFile((Component)(*storage)->myself);
  313.     require(resFile > 0, finish);
  314.     
  315.                 // Note: DO NOT call Happi version of this call.
  316.                 // It doesn't work if the DITL is loaded into temp mem.
  317.     theItems = GetResource('DITL', kPickerDITL);
  318.     check(theItems);
  319.     
  320.     if (theItems)
  321.         DetachResource(theItems);
  322.     
  323.     theErr = CloseComponentResFile(resFile);
  324.     check(theErr == noErr);
  325.     
  326. finish:
  327.     
  328.     return (SInt32)theItems;
  329. }
  330.  
  331. //---------------------------------------------------------------------  doPickerGetColor
  332.  
  333. pascal ComponentResult doPickerGetColor (PickerStorageHandle storage, ColorType whichColor, 
  334.         PMColorPtr color)
  335. {
  336.     if (whichColor == kNewColor)
  337.         *color = (*storage)->currPMColor;
  338.     else
  339.         *color = (*storage)->origPMColor;
  340.     
  341.     return noErr;
  342. }
  343.  
  344. //---------------------------------------------------------------------  doPickerSetColor
  345.  
  346. pascal ComponentResult doPickerSetColor (PickerStorageHandle storage, ColorType whichColor, 
  347.         PMColorPtr color)
  348. {    
  349.     if (whichColor == kNewColor)
  350.     {
  351.         (*storage)->wasColor = (*storage)->currPMColor;
  352.         (*storage)->currPMColor = *color;
  353.     }
  354.     else
  355.         (*storage)->origPMColor = *color;
  356.     
  357.                 // If the picker is already visible, we need to update everything.
  358.     if ((*storage)->visible)
  359.     {
  360.         SetColorPattern(storage, whichColor);
  361.         DrawColorEditor(storage, kDrawNew + kDrawOrig);    
  362.     }
  363.     
  364.     return noErr;
  365. }
  366.  
  367. //---------------------------------------------------------------------  doPickerEvent
  368.  
  369. pascal ComponentResult doPickerEvent (PickerStorageHandle storage, EventData* data)
  370. {
  371.     OSErr        theErr;
  372.     char        wasState;
  373.     
  374.     theErr = noErr;
  375.     data->handled = false;
  376.     data->action = kDidNothing;
  377.     
  378.                 // Lock and point to storage.
  379.     wasState = HGetState((Handle)storage);
  380.     HLock((Handle)storage);
  381.     
  382.     if (data->event)
  383.     {
  384.         switch (data->event->what)
  385.         {
  386.             case nullEvent:
  387.             theErr = DoIdle(storage, data);
  388.             break;
  389.             
  390.             case mouseDown:
  391.             theErr = DoMouseDown(storage, data);
  392.             break;
  393.             
  394.             case keyDown:
  395.             case autoKey:
  396.             theErr = DoKeyDown(storage, data);
  397.             break;
  398.             
  399.             case keyUp:
  400.             theErr = DoKeyUp(storage, data);
  401.             break;
  402.             
  403.             case activateEvt:
  404.             if (data->event->modifiers & activeFlag)
  405.                 ActivatePicker(storage);
  406.             else
  407.                 DeactivatePicker(storage);
  408.             break;
  409.             
  410.             case osEvt:
  411.             if (((char *)&data->event->message)[0] & suspendResumeMessage)
  412.             {
  413.                 if ( (data->event->message & resumeFlag) && 
  414.                      (FrontWindow() == (*storage)->port) )
  415.                     ActivatePicker(storage);
  416.                 else
  417.                     DeactivatePicker(storage);
  418.             }
  419.             break;
  420.             
  421.             default:
  422.             check(true);
  423.             break;
  424.         }
  425.     }
  426.     else
  427.     {
  428.                 // we got a forecast event...
  429.         switch (data->forcast)
  430.         {
  431.             case kDialogAccept:
  432.             break;
  433.                 // The default case is okay - it probably means we're switching out
  434.                 // of the picker and into another picker.
  435.             default:
  436.             break;
  437.         }
  438.     }
  439.     
  440.     HSetState((Handle)storage, wasState);
  441.     
  442.     return theErr;
  443. }
  444.  
  445. //---------------------------------------------------------------------  doPickerEdit
  446.  
  447. pascal ComponentResult doPickerEdit (PickerStorageHandle storage, EditData* data)
  448. {
  449.     PickerStoragePtr    pStorage;
  450.     char                wasState;
  451.     
  452.                 // Lock and point to storage.
  453.     wasState = HGetState((Handle)storage);
  454.     HLock((Handle)storage);
  455.     pStorage = *storage;
  456.  
  457.     switch (data->theEdit)
  458.     {
  459.         case kUndo:
  460.         (void) doPickerSetColor(storage, kNewColor, &((*storage)->wasColor));
  461.         data->action = kColorChanged;
  462.         data->handled = true;
  463.         break;
  464.         
  465.         default:
  466.         data->action = kDidNothing;
  467.         data->handled = false;
  468.         break;
  469.     }
  470.     
  471.     HSetState((Handle)storage, wasState);
  472.     
  473.     return noErr;
  474. }
  475.  
  476. //---------------------------------------------------------------------  doPickerSetVisibility
  477.  
  478. pascal ComponentResult doPickerSetVisibility (PickerStorageHandle storage, Boolean visible)
  479. {
  480.                 // Do nothing if state is the same.
  481.     if ((*storage)->visible == visible)
  482.         return noErr;
  483.     
  484.                 // Note new state.
  485.     (*storage)->visible = visible;
  486.     
  487.     if (visible)
  488.         SetColorPattern(storage, kOriginalColor);
  489.     
  490.     return noErr;
  491. }
  492.  
  493. //---------------------------------------------------------------------  doPickerDisplay
  494.  
  495. pascal ComponentResult doPickerDisplay (PickerStorageHandle storage)
  496. {
  497.     if (!(*storage)->visible)
  498.         return noErr;
  499.     
  500.     DrawColorEditor(storage, kDrawEverything);
  501.     
  502.     return noErr;
  503. }
  504.  
  505. //---------------------------------------------------------------------  doPickerItemHit
  506.  
  507. pascal ComponentResult doPickerItemHit (PickerStorageHandle storage, ItemHitData* data)
  508. {
  509.     PickerStoragePtr    pStorage;
  510.     char                wasState;
  511.     OSErr                theErr;
  512.     Rect                iBox;
  513.     Handle                theItem;
  514.     SInt16                iType, resFile;
  515.     
  516.                 // Assume no error and nothing done.
  517.     theErr = noErr;
  518.     data->action = kDidNothing;
  519.     
  520.                 // Lock and point to storage.
  521.     wasState = HGetState((Handle)storage);
  522.     HLock((Handle)storage);
  523.     pStorage = *storage;
  524.     
  525.     GetDialogItem((DialogPtr)pStorage->port, pStorage->baseItem + data->itemHit, &iType, &theItem, &iBox);
  526.     
  527.     switch (data->itemHit)
  528.     {
  529.         case iOrigColor:
  530.         theErr = DoListClick(storage, data);
  531.         break;
  532.         
  533.         case iPrevPict:
  534.         resFile = OpenScrapBookFile();
  535.         require(resFile > 0, bail);
  536.         (*storage)->numPicts = Count1Resources('PICT');
  537.         CloseResFile(resFile);
  538.         
  539. bail:
  540.         
  541.         (*storage)->pictNumber--;
  542.         if ((*storage)->pictNumber < -1)
  543.             (*storage)->pictNumber = (*storage)->numPicts;
  544.         DrawColorEditor(storage, kDrawPict);
  545.         break;
  546.         
  547.         case iNextPict:
  548.         resFile = OpenScrapBookFile();
  549.         require(resFile > 0, bail2);
  550.         (*storage)->numPicts = Count1Resources('PICT');
  551.         CloseResFile(resFile);
  552.         
  553. bail2:
  554.         
  555.         (*storage)->pictNumber++;
  556.         if ((*storage)->pictNumber > (*storage)->numPicts)
  557.             (*storage)->pictNumber = -1;
  558.         DrawColorEditor(storage, kDrawPict);
  559.         break;
  560.         
  561.         default:
  562.         break;
  563.     }
  564.     
  565.     HSetState((Handle)storage, wasState);
  566.     
  567.     return theErr;
  568. }
  569.  
  570. //---------------------------------------------------------------------  doPickerSetBaseItem
  571.  
  572. pascal ComponentResult doPickerSetBaseItem (PickerStorageHandle storage, SInt16 baseItem)
  573. {
  574.     (*storage)->baseItem = baseItem;
  575.     
  576.     return noErr;
  577. }
  578.  
  579. //---------------------------------------------------------------------  doPickerGetProfile
  580.  
  581. pascal ComponentResult doPickerGetProfile (PickerStorageHandle storage)
  582. {
  583. #pragma unused (storage)
  584.     
  585.     return 0L;
  586. }
  587.  
  588. //---------------------------------------------------------------------  doPickerSetProfile
  589.  
  590. pascal ComponentResult doPickerSetProfile (PickerStorageHandle storage, CMProfileHandle profile)
  591. {
  592. #pragma unused (storage, profile)
  593.     
  594.     return noErr;
  595. }
  596.  
  597. //---------------------------------------------------------------------  doPickerGetPrompt
  598.  
  599. pascal ComponentResult doPickerGetPrompt (PickerStorageHandle storage, Str255 prompt)
  600. {
  601.     PasStringCopy((*storage)->prompt, prompt);
  602.     
  603.     return noErr;
  604. }
  605.  
  606. //---------------------------------------------------------------------  doPickerSetPrompt
  607.  
  608. pascal ComponentResult doPickerSetPrompt (PickerStorageHandle storage, Str255 prompt)
  609. {
  610.     PasStringCopy(prompt, (*storage)->prompt);
  611.     
  612.     return noErr;
  613. }
  614.  
  615. //---------------------------------------------------------------------  doPickerGetIconData
  616.  
  617. pascal ComponentResult doPickerGetIconData (PickerStorageHandle storage, PickerIconData* data)
  618. {
  619.     PickerIconData**    pickerData;
  620.     OSErr                theErr;
  621.     SInt16                fileRef;
  622.     
  623.                 // Set up some default values.
  624.     theErr = noErr;
  625.     data->scriptCode = 0;
  626.     data->iconSuiteID = kPickerData;
  627.     
  628.                 // Open the components res fork.
  629.     fileRef = OpenComponentResFile((Component)(*storage)->myself);
  630.     require(fileRef > 0, fail);
  631.     
  632.                 // Get the picker data out.
  633.     pickerData = (PickerIconData **)HappiGetResource(kPickerDataType, kPickerData, &theErr);
  634.     require_action(pickerData, fail, (void) CloseComponentResFile(fileRef););
  635.     
  636.                 // Copy the data to our struct and dispose of the resource handle.
  637.     *data = **pickerData;
  638.     DisposeHandle((Handle)pickerData);
  639.     
  640.                 // Close res fork again.
  641.     theErr = CloseComponentResFile(fileRef);
  642.     check(theErr == noErr);
  643.     
  644.     return theErr;
  645.     
  646. fail:
  647.     
  648.     return pickerResourceError;
  649. }
  650.  
  651. //---------------------------------------------------------------------  doPickerGetEditMenuState
  652.  
  653. pascal ComponentResult doPickerGetEditMenuState (PickerStorageHandle storage, 
  654.         PickerMenuState* mState)
  655. {
  656. #pragma unused (storage)
  657.     
  658.     SInt32        scrapOffset;
  659.     
  660.     mState->cutEnabled = true;
  661.     mState->copyEnabled = true;
  662.     (void) TEToScrap();
  663.     mState->pasteEnabled = (GetScrap(0L, 'TEXT', &scrapOffset) > 0L);
  664.     mState->clearEnabled = true;
  665. //    mState->undoEnabled = (*storage)->dirty;
  666.     
  667.     GetIndString(mState->undoString, kPickerMiscStrsID, kUndoStringIndex);
  668.     
  669.     return noErr;
  670. }
  671.  
  672. //---------------------------------------------------------------------  doPickerSetOrigin
  673.  
  674. pascal ComponentResult doPickerSetOrigin (PickerStorageHandle storage, Point where)
  675. {
  676. #pragma unused (storage, where)
  677.     
  678.     return noErr;
  679. }
  680.  
  681. //---------------------------------------------------------------------  doPickerExtractHelpItem
  682.  
  683. pascal ComponentResult doPickerExtractHelpItem (PickerStorageHandle storage, SInt16 itemNo, 
  684.         SInt16 whichState, HelpItemInfo* helpInfo)
  685. {
  686.     OSErr        theErr, wasErr;
  687.     SInt16        resFile, count;
  688.     
  689.     theErr = noHelpForItem;
  690.     wasErr = noErr;
  691.     
  692.     itemNo -= (*storage)->baseItem;
  693.     
  694.     resFile = OpenComponentResFile((Component)(*storage)->myself);
  695.     require(resFile > 0, bail);
  696.     
  697.     theErr = HMGetIndHelpMsg(kHMDialogResType, kPickerHelpID, itemNo, whichState, 
  698.             (UInt32 *)&helpInfo->options, &helpInfo->tip, 
  699.             &helpInfo->altRect, &helpInfo->theProc, &helpInfo->helpVariant, 
  700.             &helpInfo->helpMessage, &count);
  701.     
  702.                 // If item skipped for balloon help, report back as noHelpForItem.
  703.     if (theErr == hmSkippedBalloon)
  704.         theErr = noHelpForItem;
  705.     
  706.     check((theErr == noErr) || (theErr == noHelpForItem));
  707.     
  708.     if (theErr != noErr)
  709.         wasErr = theErr;
  710.     theErr = CloseComponentResFile(resFile);
  711.     check(theErr == noErr);
  712.     
  713.     if ((theErr == noErr) && (wasErr != noErr))
  714.         theErr = wasErr;
  715.     
  716. bail:
  717.     
  718.     return theErr;
  719. }
  720.  
  721. #if NEW_COLORPICKER_2_1_CALLS
  722.  
  723. //---------------------------------------------------------------------  doPickerSetColorChangedProc
  724.  
  725. pascal ComponentResult doPickerSetColorChangedProc (PickerStorageHandle storage, ColorChangedUPP colorProc, long colorProcData)
  726. {
  727.     (*storage)->colorProc = colorProc;
  728.     (*storage)->colorProcData = colorProcData;
  729.     
  730.     return noErr;
  731. }
  732.  
  733. //---------------------------------------------------------------------  doNPickerGetColor
  734.  
  735. pascal ComponentResult doNPickerGetColor (PickerStorageHandle storage, ColorType whichColor, NPMColorPtr color)
  736. {
  737.     PickerStoragePtr    pStorage;
  738.     char                wasState;
  739.     OSErr                theErr;
  740.     
  741.                 // Check we were called through new API
  742.     if (!(*storage)->calledNPickColor
  743.         return cantLoadPicker;
  744.     
  745.     theErr = noErr;
  746.  
  747.                 // Lock and point to storage.
  748.     wasState = HGetState((Handle)storage);
  749.     HLock((Handle)storage);
  750.     pStorage = *storage;
  751.     
  752.     if (whichColor == kNewColor)
  753.     {
  754.         *color = pStorage->currNPMColor;
  755.         
  756.                 // If user changed something…
  757.         if (pStorage->dirty)
  758.         {
  759.             color->color.rgb = pStorage->currColor;
  760.             PickerMatchColors( &(pStorage->PickerToNColorCW), &(color->color), 1);
  761.         }
  762.     }
  763.     else
  764.         *color = pStorage->origNPMColor;
  765.     
  766.     HSetState((Handle)storage, wasState);
  767.     
  768.     return theErr;
  769. }
  770.  
  771. //---------------------------------------------------------------------  doNPickerSetColor
  772.  
  773. pascal ComponentResult doNPickerSetColor (PickerStorageHandle storage, ColorType whichColor, NPMColorPtr color)
  774. {
  775.     CMPickerColor        undoColor;
  776.     
  777.                 // Remember we were called through new API
  778.     (*storage)->calledNPickColor = true;
  779.     
  780.     if (whichColor == kNewColor)
  781.     {
  782.         undoColor = (*storage)->currColor;
  783.         (*storage)->currNPMColor = *color;
  784.     }
  785.     else
  786.         (*storage)->origNPMColor = *color;
  787.     
  788.                 // If the picker is already visible, we need to update everything.
  789.     if ((*storage)->visible)
  790.     {
  791.         PickerStoragePtr    pStorage;
  792.         CMColor                tempColor;
  793.         char                wasState;
  794.         
  795.                 // Lock and point to storage.
  796.         wasState = HGetState((Handle)storage);
  797.         HLock((Handle)storage);
  798.         pStorage = *storage;
  799.         
  800.         if (whichColor == kNewColor)
  801.         {
  802.             tempColor = pStorage->currNPMColor.color;
  803.             PickerMatchColors( &(pStorage->NColorToPickerCW), &tempColor, 1);
  804.             pStorage->undoColor = pStorage->currColor = tempColor.rgb;
  805.             GetUserValsFromColor((CMColor*)&pStorage->currColor, (CMColor*)&pStorage->currUserColor, 
  806.                     kUserColorSpace);
  807.             
  808.                 // The color just lost moves into undo.
  809.             pStorage->undoColor = undoColor;
  810.             
  811.                 // Now update everything.
  812.             DrawColorEditor(storage, kDrawEverything);
  813.             
  814.             pStorage->wasUserColor = pStorage->currUserColor;
  815.         }
  816.         else
  817.         {
  818.             tempColor = pStorage->origNPMColor.color;
  819.             #if ASSUME_CURR_ORIG_PROFS_SAME
  820.             PickerMatchColors( &(pStorage->NColorToPickerCW), &tempColor, 1);
  821.             #else
  822.             PickerMatchColors( &(pStorage->OColorToPickerCW), &tempColor, 1);
  823.             #endif
  824.             pStorage->origColor = tempColor.rgb;
  825.             GetUserValsFromColor((CMColor*)&pStorage->origColor,
  826.                                  (CMColor*)&pStorage->origUserColor, kUserColorSpace);
  827.             
  828.                 // Update the internal pattern.
  829.             SetColorPattern(storage, kOriginalColor);
  830.             
  831.                 // Now update just the list.
  832.             DrawColorEditor(storage, kDrawNew+kDrawOrig);
  833.         }
  834.         
  835.         HSetState((Handle)storage, wasState);
  836.     }
  837.     
  838.     return noErr;
  839. }
  840.  
  841. //---------------------------------------------------------------------  doNPickerGetProfile
  842.  
  843. pascal ComponentResult doNPickerGetProfile (PickerStorageHandle storage, CMProfileRef *profile)
  844. {
  845.                 // Check we were called through new API
  846.     if (!(*storage)->calledNPickColor
  847.         return cantLoadPicker;
  848.     
  849.                 // Return the stored the ProfileLocation Ptr.
  850.     *profile = (*storage)->profileRef;
  851.  
  852.     return noErr;
  853. }
  854.  
  855. //---------------------------------------------------------------------  doNPickerSetProfile
  856.  
  857. pascal ComponentResult doNPickerSetProfile (PickerStorageHandle storage, CMProfileRef profile)
  858. {
  859.                 // Remember we were called through new API
  860.     (*storage)->calledNPickColor = true;
  861.  
  862.                 // Store the ProfileLocation Ptr.
  863.     (*storage)->profileRef = profile;
  864.  
  865.     return noErr;
  866. }
  867.  
  868. //---------------------------------------------------------------------  doPickerSetColorChangedProc
  869.  
  870. pascal ComponentResult doNPickerSetColorChangedProc (PickerStorageHandle storage, NColorChangedUPP colorProc, long colorProcData)
  871. {
  872.     (*storage)->ncolorProc = colorProc;
  873.     (*storage)->colorProcData = colorProcData;
  874.     
  875.     return noErr;
  876. }
  877.  
  878. #endif            // NEW_COLORPICKER_2_1_CALLS
  879.  
  880.  
  881.